package com.acme.team03.net.server;

import java.io.IOException;
import java.net.ServerSocket;

import com.acme.team03.net.ServerMessageHandler;

public class ChatServer implements Runnable {

	private ServerSocket socketFactory;
	private ConnectionDispatcher connDisp;
	private boolean isStopped;
	private ServerMessageHandler msgHandler;
	private int nextConnID = 1;

	public ChatServer() {
		connDisp = new ConnectionDispatcher();
		msgHandler = new ServerMessageHandler(connDisp);
		new Thread(msgHandler).start();
	}

	public static void main(String[] args) {
		ChatServer chatServer = new ChatServer();
		chatServer.run();
	}

	public void run() {
		try {
			socketFactory = new ServerSocket(7777);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (!isStopped) {
			try {
				connDisp.establishConnection(nextConnID++, socketFactory.accept(), msgHandler);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
